<?php namespace Phpcmf\Controllers\Admin;

// 优速贡献代码
class Sendtpl extends \Phpcmf\App
{

    // 来自cms的群发
    public function show_add() {

        $id = (int)\Phpcmf\Service::L('Input')->get('id');
        $mid = dr_safe_filename(\Phpcmf\Service::L('Input')->get('mid'));
        if (!$id || !$mid) {
            $this->_json(0, '参数不能为空');
        }

        $this->_module_init($mid);
        $data = $this->content_model->get_data($id);
        if (!$data) {
            $this->_json(0, '内容不存在');
        }

        $file = WRITEPATH.'config/weixin_sendtpl.php';
        $config = [];
        if (is_file($file)) {
            $config = require $file;
        }

        if (!isset($config[$mid])) {
            $this->_json(0, '微信插件-内容模板群发设置-没有设置本模块参数');
        } elseif (!isset($config[$mid]['use']) || !$config[$mid]['use']) {
            $this->_json(0, '微信插件-内容模板群发设置-没有开启本模块');
        } elseif (!isset($config[$mid]['id']) || !$config[$mid]['id']) {
            $this->_json(0, '微信插件-内容模板群发设置-没有设置本模块参数');
        } elseif (!isset($config[$mid]['content']) || !$config[$mid]['content']) {
            $this->_json(0, '微信插件-内容模板群发设置-没有设置本模块参数');
        }


        $cache = \Phpcmf\Service::M()->table(weixin_wxtable('user'))->getAll();
        if (!$cache) {
            $this->_json(0, dr_lang('没有可用粉丝'));
        }

        // 生成模板
        $template = [];
        foreach ($config[$mid]['content'] as $t) {
            if ($t['name']) {
                $value = $t['value'];
                $tpl = $this->replace($data, $value);
                $template[$t['name']] = [
                    'value' => $tpl ? $tpl : $value,
                    'color' => $t['color'],
                ];
            }
        }
        if (!$template) {
            $this->_json(0, dr_lang('模板内容解析后不存在'));
        }

        $data['template'] = $config[$mid]['id'];
        $data['color'] = $config[$mid]['color'];
        $data['url'] = dr_url_prefix($data['url'], $mid);

        // 存储文件
        \Phpcmf\Service::L('cache')->set_data('app-weixin-template-send', [
            'data' => $data,
            'template' => $template,
            'users' => dr_save_bfb_data($cache)
        ], 3600);

        $this->_json(1, 'ok', ['url' => dr_url('weixin/template/show_index', ['counts'=> count($cache)])]);
    }

    // 查看列表
    public function index() {

        $file = WRITEPATH.'config/weixin_sendtpl.php';
        $module = \Phpcmf\Service::L('cache')->get('module-'.SITE_ID.'-content');

        if (IS_POST) {
            $post = \Phpcmf\Service::L('input')->post('data');
            \Phpcmf\Service::L('Config')->file($file, '微信模板群发', 32)->to_require($post);
            $this->_json(1, '操作成功');
        }

        $one = reset($module);
        $page = \Phpcmf\Service::L('input')->get('page');
        if (!$page) {
            $page = $one['dirname'];
        }

        \Phpcmf\Service::V()->assign([
            'page' => $page,
            'data' => is_file($file) ? require $file : [],
            'form' => dr_form_hidden(),
            'menu' => \Phpcmf\Service::M('auth')->_admin_menu(
                [
                    '模块内容群发' => [APP_DIR.'/'.\Phpcmf\Service::L('Router')->class.'/index', 'fa fa-edit'],
                    'help' => [1127],
                ]),
            'module' => $module,
        ]);
        \Phpcmf\Service::V()->display('sendtpl.html');
    }


    // 替换全部
    private function replace($data, $value) {

        $rep = new \php5replace($data);
        $value = preg_replace_callback('#{([A-Z_]+)}#U', array($rep, 'php55_replace_var'), $value);
        $value = preg_replace_callback('#{([a-z_0-9]+)}#U', array($rep, 'php55_replace_data'), $value);
        $value = preg_replace_callback('#{\$([a-z_0-9]+)}#U', array($rep, 'php55_replace_data'), $value);
        $value = preg_replace_callback('#{([a-z_0-9]+)\((.*)\)}#Ui', array($rep, 'php55_replace_function'), $value);

        return $value;
    }

}
